<?php
/**
 * 新闻资讯模块
 **/
namespace Mama\Controller;
use Think\Controller;

class WeixinController extends Controller {
	/**
	 * 新闻详情
	 **/
    public function index($newid){
		//获取新闻详情
		$info = D('News')->getNewsInfoById($newid);
		$this->assign('info', $info);
		$this->display('Weixin/info');
    }
	
	/**
	 * 获取招聘列表
	 **/
	public function recruit(){
		$id = I('get.id', 0);
		if(empty($id)){
			$recruitList = M('recruit')->where(array('visible' => 1))->order('id asc')->select();
			$this->assign('recruit', $recruitList);
			$this->display('Weixin/recruit');
		}else{
			$info = M('recruit')->where(array('visible' => 1,'id' => $id))->find();
			$this->assign('info', $info);
			$this->display('Weixin/recruit_info');
		}
	}
	
	/**
	 * 登陆操作
	 **/
	public function dologin(){
		if(IS_POST){
			$username = I('post.username', '');
			$password = I('post.password', '');
			//根据用户帐号获取信息
			$logtype = I('post.ltype', 1); //登陆方式 1：自建帐号登陆 2：三方帐号登陆
			$memberDao = D('Member');
			$returnMessage = array(
				'errCode' => 0,
				'errMsg' => '登陆失败'
			);
			if($logtype == 1){
				$uinfo = $memberDao->getMemberInfoByUsername($username);
				if($uinfo && $uinfo['password'] == md5($password)){
					session('wxuserid', $uinfo['userid']);
					session('wxnickname', $uinfo['nickname']);
					session('wxphone', $uinfo['phone']);
					session('wxface', $uinfo['login_face']);
					//设置跳转地址
					if($uinfo['roleid']){
						$returnMessage['errCode'] = 1;
					}else{
						$returnMessage['errCode'] = 2;
					}
					$returnMessage['errMsg'] = '登陆成功';
				}else{
					$returnMessage['errCode'] = 0;
					$returnMessage['errMsg'] = '登陆密码错误';
				}
			}else{
				$uinfo = $memberDao->getMemberInfoByOtherName($username);
				session('wxuserid', $uinfo['userid']);
				session('wxnickname', $uinfo['nickname']);
				session('wxphone', $uinfo['phone']);
				session('wxface', $uinfo['login_face']);
			}
			
			echo json_encode($returnMessage);
		}
	}
	
	/**
	 * 用户注册
	 **/
	public function register(){
		if(IS_GET){
			$this->display();
		}else{
			$data = I('post.');
			//判断是否存在用户
			$memberDao = D('Member');
			$info = $memberDao->getMemberInfoByUsername($data['username']);
			if(count($info)){
				echo 2;
			}else{
				$data['password'] = md5($data['password']);
				$data['phone'] = $data['username'];
				$data['roleid'] = 0;
				$data['nickname'] = $data['username'];
				$data['createtime'] = time();
				$data['admin_id'] = 0;
				$data['admin_nick'] = '自主注册';
				unset($data['code']);
				$row = $memberDao->registerMemberInfo($data);
				if($row !== false){
					session('wxuserid', $row);
					session('wxnickname', $data['username']);
					session('wxphone', $data['username']);
					session('wxface', '');
					echo 1;
				}else{
					echo 0;
				}
			}
		}
	}
	
	public function welcome(){
		$this->display();
	}
	
	public function create(){
		$this->display('Weixin/create');
	}
	
	/**
	 * 个人中心
	 **/
	public function ucenter(){
		$userid = session('wxuserid');
		if($userid){
			$func = I('func', '');
			switch($func){
				case 'pwdset': //密码设置
					$this->display('UCenter/pwdset');
				break;
				case 'pkey': //电子钥匙
					$this->display('UCenter/pkey');
				break;
				case 'cost': //账单查询
					$this->display('UCenter/cost');
				break;
				case 'pay': //缴费
					$this->display('UCenter/pay');
				break;
				case 'baojie': //保洁
					$this->display('UCenter/baojie');
				break;
				case 'weixiu': //维修
					$this->display('UCenter/weixiu');
				break;
				case 'yuyue': // 预约
					$this->display('UCenter/yuyue');
				break;
				case 'look': //看房记录
					$this->display('UCenter/look');
				break;
				case 'goback': //退房
					$this->display('UCenter/goback');
				break;
				case 'gobacklog':
					$this->display('UCenter/gobacklog');
				break;
				case 'order':
					$this->display('UCenter/order');
				break;
				case 'kaipiao': //开票信息列表
					if(IS_GET){
						$list = D('Ucenter')->getInvoiceListByUserId($userid);
						$this->assign('invoice', $list);
						$this->display('UCenter/kaipiao');
					}else{ //设置默认
						$invoice_id = I('post.invoice_id', 0);
						$row = D('Ucenter')->editInvoiceDefaultByInvoiceId($invoice_id, $userid);
						if($row !== false){
							echo 1;
						}else{
							echo 0;
						}
					}
				break;
				case 'kaipiaoinfo': //发票详情
					if(IS_GET){
						$invoice_id = I('invid', 0);
						if(!empty($invoice_id)){
							$info = D('Ucenter')->getInvoiceInfoByInvoiceId($invoice_id);
							$this->assign('info', $info);
						}
						
						$this->display('UCenter/addkaipiao');
					}else{
						$data = I('post.');
						unset($data['func']);
						$invoice_id = $data['invoice_id'];
						if(!empty($data['is_default'])){
							//清空默认设置
							D('Ucenter')->clearInvoiceDefault($userid);
						}
						//判断类型
						if(empty($invoice_id)){
							$data['userid'] = $userid;
							$data['createtime'] = time();
							$row = D('Ucenter')->addInvoiceInfo($data);
						}else{
							$row = D('Ucenter')->editInvoiceInfoByInvoiceId($invoice_id, $data);
						}
						
						if($row !== false){
							echo 1;
						}else{
							echo 0;
						}
					}
				break;
				case 'openlog':
					$this->display('UCenter/openlog');
				break;
				default:
					$uinfo = D('Member')->getMemberInfoByUserid($userid);
					$this->assign('uinfo', $uinfo);
					$this->display();
				break;
			}
		}else{
			$this->display('Weixin/login');
		}
	}
	
	/**
	 * 社区活动
	 **/
	public function event(){
		$this->display('Weixin/event');
	}

	public function eventinfo(){
		$this->display('Weixin/eventinfo');
	}

	/**
	 * 我要租房
	 **/
	public function village(){
		//获取城市名称
		$city = I('get.city', '重庆');
		//根据城市名称获取小区标识
		$regionDao = D('Region');
		$regionInfo = $regionDao->getRegionInfoByTitle($city);
		if(count($regionInfo)){
			$regionid = $regionInfo['region_id'];
		}else{
			$regionid = 1;
		}
		//根据小区查询分店标识
		$villagelist = D('Village')->getVillageListByRegionId($regionid);
		$this->assign('villagelist', $villagelist);
		$this->assign('city', $city);
		
		$this->display('Weixin/village');
	}
	
	/**
	 * 开通城市选择
	 **/
	public function select(){
		$city = I('get.city', '重庆');
		//获取开通的城市信息
		$regionDao = D('Region');
		$citylist = $regionDao->getRegionList(0);
		$this->assign('citylist', $citylist);
		
		$this->assign('city', $city);
		$this->display('Weixin/select');
	}
	
	/**
	 * 预约看房
	 **/
	public function yuyue(){
		//判断是否已经登陆
		$wxuserid = session('wxuserid');
		$wxuserid = empty($wxuserid) ? 0 : $wxuserid;
		if($wxuserid){
			//获取房间ID
			$roomid = I('get.roomid', 0);
			//获取房间信息
			//获取房间详情
			$roominfo = D('Room')->getRoomInfoByRoomId($roomid);
			//查询房间价格
			$roominfo['rent'] = '1500.00';
			$this->assign('roominfo', $roominfo);
			$villageinfo = D('Village')->getVillageInfoByVillageId($roominfo['villageid']);
			$this->assign('villageinfo', $villageinfo);
			
			$this->display('Weixin/yuyue');
		}else{
			//跳转到登陆界面
			$this->display('Weixin/login');
		}
	}

	/**
	 * 立即租房
	 **/
	public function rental(){
		$wxuserid = session('wxuserid');
		$wxuserid = empty($wxuserid) ? 0 : $wxuserid;
		if($wxuserid){
			//获取房间ID
			$roomid = I('get.roomid', 0);
			//获取房间信息
			//获取房间详情
			$roominfo = D('Room')->getRoomInfoByRoomId($roomid);
			//查询房间价格
			$roominfo['rent'] = '1500.00';
			$this->assign('roominfo', $roominfo);
			$villageinfo = D('Village')->getVillageInfoByVillageId($roominfo['villageid']);
			$this->assign('villageinfo', $villageinfo);

			$this->display('Weixin/rental');
		}else{
			//跳转到登陆界面
			$this->display('Weixin/login');
		}	
	}

	/**
	 * 房间列表
	 **/
	public function room(){
		//获取小区ID
		$villageid = I('get.vid', 0);
		//查询房间列表
		$roomlist = D('Room')->getRoomListByVillageId($villageid);
		$this->assign('roomlist', $roomlist);
		//获取小区信息
		$villageinfo = D('Village')->getVillageInfoByVillageId($villageid);
		$this->assign('villageinfo', $villageinfo);
		
		$this->display('Weixin/look');
	}

	/**
	 * 获取房间详情
	 **/
	public function roominfo(){
		$roomid = I('get.roomid', 0);
		//获取房间详情
		$roominfo = D('Room')->getRoomInfoByRoomId($roomid);
		//查询房间价格
		$roominfo['rent'] = '1500.00';

		$this->assign('roominfo', $roominfo);
		$this->display('Weixin/roominfo');
	}

	/**
	 * 投诉建议
	 **/
	public function tousu(){
		$this->display();
	}

	/**
	 * 嘛嘛服务
	 **/
	public function service(){
		$wxuserid = session('wxuserid');
		$wxuserid = empty($wxuserid) ? 0 : $wxuserid;
		if($wxuserid){
			$uinfo = D('Member')->getMemberInfoByUserid($userid);
			$this->assign('uinfo', $uinfo);
			$this->display('Weixin/ucenter');
		}else{
			$this->display('Weixin/login');
		}
	}
}
